package com.util;

import org.junit.jupiter.api.Test;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class DateAndTime {

    /**
     * 获取200秒前的时间
     */
    @Test
    public void test1(){
        String now= LocalDateTime.now().toString();
        System.out.println(now);
        String before= LocalDateTime.now().minusSeconds(200).toString();
        System.out.println(before);
    }

    /**
     * Date 转为 localDateTime
     */
    @Test
    public void test2(){
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(new Date().toInstant(),zoneId);
        System.out.println(localDateTime.toString());
    }

    /**
     * Date 和 localDateTime 类型比较
     */
    @Test
    public void test3(){
        Date date=new Date();
        System.out.println(date.getTime());
        LocalDateTime dateTime=LocalDateTime.now();
        long timestamp = Timestamp.valueOf(dateTime).getTime();
        System.out.println(timestamp);
    }


    /**
     * String 转为 localDate 时间
     */
    @Test
    public void test4(){
        DateTimeFormatter struct = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate localDate = LocalDate.now();
        String format = struct.format(localDate);

        System.out.println("LocalDate转成String类型的时间："+format);
        LocalDate parse = LocalDate.parse("2023-11-3");
      System.out.println("String类型的时间转成LocalDateTime："+parse);
    }


    /**
     * String 转为 date 时间
     */
    @Test
    public void test5() throws ParseException {
        DateFormat fmt =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = fmt.parse("2023-11-06 15:36:28");
        System.out.println(date);
    }

}
